//
//  YSGMeetingViewController.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2022/9/16.
//  Copyright © 2022 tao Tao. All rights reserved.
//

import UIKit
import SnapKit

class YSGMeetingViewController: CLBaseViewController {
    override init() {
        super.init()
        modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public var status: YSGMeetingStatus = .none {
        didSet {
            HUDTool.hideActivity()
            switch status {
            case .none:
                break
            case .disConnected:
                break
            case .connected:
                loadingView.removeFromSuperview()
                otherVideoView.snp.remakeConstraints { make in
                    make.edges.equalToSuperview()
                }
                mineVideoView.snp.remakeConstraints { make in
                    make.right.equalTo(-15)
                    make.top.equalTo(clSafeAreaInsets.top + 15)
                    make.width.equalTo(videoViewSize.width)
                    make.height.equalTo(videoViewSize.height)
                }
            case .connecting:
                otherVideoView.snp.remakeConstraints { make in
                    make.edges.equalToSuperview()
                }
                mineVideoView.snp.remakeConstraints { make in
                    make.edges.equalToSuperview()
                }
            }
        }
    }
    /// 我的视频
    public let mineVideoView = XYVideoView()
    /// 他人视频
    public let otherVideoView = XYVideoView()
    /// 小图视频宽度
    private let videoViewSize = CGSize(width: UIScreen.main.bounds.size.width * 3 / 16, height: UIScreen.main.bounds.size.width / 3) // width / 3 * 9 / 16
    
    private let loadingView = YSGMeetingLoadingView()
    /// 点击事件
    private let actionView = YSGMeetingActionView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        fd_prefersNavigationBarHidden = true
        MeetingManager.shared.meetingViewController = self
        initLayout()
        MeetingManager.shared.manager?.startVideo()
        MeetingManager.shared.manager?.setLocalVideo()
        HUDTool.showActivity()
    }
    
    func initLayout() {
        view.addSubview(otherVideoView)
        otherVideoView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(mineVideoView)
        mineVideoView.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.top.equalTo(clSafeAreaInsets.top + 15)
            make.width.equalTo(videoViewSize.width)
            make.height.equalTo(videoViewSize.height)
        }
        
        view.addSubview(loadingView)
        loadingView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(actionView)
        actionView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-50)
        }
        
        actionView.handup.addTarget(self, action: #selector(handUpButtonClick), for: .touchUpInside)
        actionView.audio.addTarget(self, action: #selector(audioButtonClick(button: )), for: .touchUpInside)
        actionView.mute.addTarget(self, action: #selector(muteButtonClick(button: )), for: .touchUpInside)
        actionView.camera.addTarget(self, action: #selector(cameraButtonClick(button: )), for: .touchUpInside)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        actionView.isHidden.toggle()
    }
}

extension YSGMeetingViewController {
    @objc func handUpButtonClick() {
        MeetingManager.shared.manager?.hangup()
        dismiss(animated: true)
    }
    @objc func audioButtonClick(button: UIControl) {
        button.isSelected.toggle()
        MeetingManager.shared.manager?.audio(enable: !button.isSelected)
    }
    @objc func muteButtonClick(button: UIControl) {
        button.isSelected.toggle()
        MeetingManager.shared.manager?.mute(enable: !button.isSelected)
    }
    @objc func cameraButtonClick(button: UIControl) {
        button.isSelected.toggle()
        MeetingManager.shared.manager?.camera(enable: button.isSelected)
    }
}
